package org.example.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import org.example.user.domain.Logs;
import org.example.user.service.LogsService;
import org.example.user.utils.JwtUtils;
import org.example.user.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("logs")
public class LogsController {
    @Autowired
    private LogsService logsService;
    @Autowired
    private RedisTemplate redisTemplate;
    @RequestMapping("list")
    public Result list(Integer current,Integer size,HttpServletRequest request) {
        String ip = request.getRemoteAddr();
        PageHelper.startPage(current,size);
        String token = (String) redisTemplate.opsForValue().get("token"+ip);
        Map map = (Map) JwtUtils.parseToken(token);
        String  username = (String) map.get("username");
        LambdaQueryWrapper<Logs> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Logs::getUsername,username);
        List<Logs> list1 = logsService.list(wrapper);
//                List<Logs> list1 = logsService.list();
return Result.success(new PageInfo<>(list1));

    }

    @RequestMapping("t1")
    public  String t1(HttpServletRequest request) {
        String ip = request.getRemoteAddr();
        String token = (String) redisTemplate.opsForValue().get("token"+ip);
        Map map = (Map) JwtUtils.parseToken(token);
        String  username = (String) map.get("username");
        return username;
    }
}
